package com.nineclock.document.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.document.DocumentControllerApi;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.dto.DocFolderDto;
import com.nineclock.document.dto.DocumentDto;
import com.nineclock.document.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DocumentController implements DocumentControllerApi {
    @Autowired
    private DocumentService documentService;

    /**
     * 根据文档id查询文档历史记录
     *
     * @param fileId
     * @return
     */
    @GetMapping("/history/{fileId}")
    public Result<List<DocFileHistoryDto>> queryDocHistory(@PathVariable String fileId) {
        List<DocFileHistoryDto> docFileHistoryDtos = documentService.queryDocHistory(fileId);
        return Result.success(docFileHistoryDtos);
    }

    /**
     * @description: 分页查询文件列表
     * @param: page
     * pageSize
     * keyword
     * @return: com.nineclock.common.entity.Result<com.nineclock.common.entity.PageResult < com.nineclock.document.dto.DocFileDto>>
     * @author 菜鸟
     * @date: 2021/7/22
     */
    @Override
    @GetMapping("/pagingFile")
    public Result<PageResult<DocFileDto>> PagingFile(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     String keyword) {
        PageResult<DocFileDto> pageResult = documentService.PagingFile(page, pageSize, keyword);
        return Result.success(pageResult);
    }

    /**
     * @description: 封禁/解封文件
     * @param: null
     * @return:
     * @author 菜鸟
     * @date: 2021/7/22
     */
    @PutMapping("/{field}/{status}")
    public Result fieldStatus(@PathVariable("field") Long field, @PathVariable("status") Short status) {


        documentService.fieldStatus(field, status);
        return Result.success();
    }

    /**
     * @description: app端文档中心查看文档
     * @param: parentFoldId
     * @return: com.nineclock.common.entity.Result<DocumentDto>
     * @author 菜鸟
     * @date: 2021/7/22
     */
    @GetMapping("/listFolderAndFile")
    public Result<DocumentDto> listFolderAndFile(@RequestParam("parentFoldId") Long parentFoldId) {
        DocumentDto documentDtos = documentService.selectDepartment(parentFoldId);
        return Result.success(documentDtos);
    }

    /**
     * @description: 文件创建
     * @param: docFileDto
     * @return: com.nineclock.common.entity.Result
     * @author 菜鸟
     * @date: 2021/7/22
     */
    @Override
    @PostMapping("/insertFile")
    public Result insertFile(@RequestBody DocFileDto docFileDto) {
        documentService.insertFile(docFileDto);
        return Result.success();
    }

    /**
     * @description: 文件夹创建
     * @param: docFolderDto
     * @return: com.nineclock.common.entity.Result
     * @author 菜鸟
     * @date: 2021/7/22
     */
    @Override
    @PostMapping("/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto docFolderDto) {
        documentService.insertFolder(docFolderDto);
        return Result.success();
    }
}
